layui.use(['table', 'jquery','element','layer','form','upload','laydate'], function () {
    var table = layui.table;
    var $ = layui.jquery;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var laydate =layui.laydate;
    laydate.render({
        elem: '#startDate' //指定元素
    });

    laydate.render({
        elem: '#endDate' //指定元素
    });

    var  orderStatus = {
        "0" : "订单取消",
        "1" : "待接单",
        "2" : "待取货",
        "3" : "待审核",
        "4" : "审核驳回",
        "5" : "业务员确认",
        "6" : "订单完成"
    }

    //第一个实例
    table.render({
        elem: '#table',
        height: 600,
        toolbar :'#toolbar',
        defaultToolbar : []
        , url: ctx + '/eng/waste/order/listPage' //数据接口
        , page: true
        , cols: [[
            {field: 'orderCode', title: '订单编码', align: 'center',width:'13%',fixed:'left'}
            , {field: 'name', title: '客户姓名', align: 'center',width:'8%',fixed:'left'}
            , {field: 'phone', title: '客户电话', align: 'center',width:'8%',fixed:'left'}
            , {field: 'recName', title: '接单人', align: 'center',width:'8%'}
            , {field: 'recPhone', title: '接单人电话', align: 'center',width:'10%'}
            , {field: 'appointTime', title: '预约时间', align: 'center',width:'12%'}
            , {field: 'createTime', title: '创建时间', align: 'center',width:'12%'}
            , {field: 'wasteTypeName', title: '废品类型', align: 'center',width:'10%'}
            , {field: 'totalPrice', title: '订单总价', align: 'center',width:'7%'}
            , {field: 'pickAddress', title: '地址', align: 'center',width:'15%'}
            , {field: 'order_type', title: '下单类型', align: 'center',width:'8%',templet:function(d){
                    if(d.orderType == 1){
                        return "估量下单";
                    }else{
                        return "明细下单";
                    }
                }}
            , {field: 'totalNum', title: '总数', align: 'center',width:'5%'}
            , {field: 'unit', title: '计量单位', align: 'center',width:'7%'}
            , {field: 'status', title: '订单状态',fixed:'right', align: 'center',width:'10%',templet:function(d){
                    return orderStatus[d.status];
                }}
            , {field: 'ops', title: '操作', width: '20%', align: 'center',toolbar: '#tool',fixed:'right'}
        ]]
    });



    // 条件查询
    $('#submit').click(function(){
        // 重新渲染数据
        table.reload('table', {
            url: ctx + '/eng/waste/order/listPage'
            ,where: $('#queryForm').formToObject()
        });
        return false;
    });
    var toolData = null;
    // tool
    table.on('tool(table)', function(obj){
        toolData = obj.data;
        var layEvent = obj.event;

        if(layEvent === 'detail'){
            detail(toolData);
        }else if(layEvent == 'assign'){
            layer.open({
                type : 1,
                content : $('#empSelectLayer'),
                title : "派单选择",
                area :['750px',"550px"],
                btn :['确定'],
                yes : function(index){
                    // 选择一个人
                    var data = table.checkStatus('empSelectTable');
                    if(data.length == 0){
                        layer.msg('请选择一个业务员',{icon:2});
                        return false;
                    }
                    $.ajax({
                        url : ctx+'/api/wasteUserOrder/manageOrder',
                        type :'post',
                        data:{
                            orderCode : toolData.orderCode,
                            openId : data.data[0].openId
                        },
                        async : false,
                        success : function(res){
                            if(res.success){
                                layer.closeAll()
                                layer.msg('派单成功',{icon:1})
                                table.reload('table')
                            }else{
                                layer.msg(res.msg,{icon:2})
                            }
                        }
                    });

                }
            });

            // 数据初始化
            table.render({
                elem: '#empSelectTable'
                ,url: '/eng/system/emp/listPage',
                page: true,
                height : '500px'
                ,cols: [[
                    {field: 'id', title: 'ID', width:180,type : 'radio'}
                    ,{field: 'name', title: '员工姓名', width:220}
                    ,{field: 'phone', title: '手机号码', width:180}
                    ,{field: 'empCode', title: '员工号(账号)', width:200}
                    ,{field: 'openId', title: 'openId', width:200,hide : true}
                ]],

            });
        }else if(layEvent == 'verify'){
            // 审核检查
            verify(toolData);
        }
    });

    var verifyData = null;
    // 审核操作
    function verify(data){
        verifyData = data;
        // 渲染内容
        $.ajax({
            url : ctx+"/eng/waste/order/getAllVerifyAdvice",
            data : {
                orderCode : data.orderCode
            },
            async : false,
            success : function(res){
                $.each(res.data,function(index,val){
                    $('#verify_item_title').html(val.createTime + "   "+val.createName);
                    $('#verify_item_content').html(val.orderRemark);
                    $('#verifyLayer ul').append($('#verify_item').html());
                });
            }
        });
        layer.open({
            type : 1,
            title :'审核详情',
            content : $('#verifyLayer'),
            area :['500px','350px'],
            btn :['通过','驳回'],
            yes :function(index){
                layer.prompt({
                    formType: 2,
                    value: '审核通过',
                    title: '输入意见',
                }, function(value, index, elem){

                    $.ajax({
                        url : ctx+"/eng/waste/order/verifyOk",
                        data :{
                            orderCode : verifyData.orderCode,
                            orderRemark : value
                        },
                        type :'post',
                        async : false,
                        success : function(res){
                            if(res.success){
                                layer.msg(res.msg,{icon:1})
                                layer.closeAll()
                                table.reload('table')
                            }else{
                                layer.msg(res.msg,{icon:2})
                            }
                        }
                    });

                });
            },
            btn2 : function(index){
                layer.prompt({
                    formType: 2,
                    value: '审核驳回',
                    title: '输入意见',
                }, function(value, index, elem){

                    $.ajax({
                        url : ctx+"/eng/waste/order/verifyFail",
                        data :{
                            orderCode : verifyData.orderCode,
                            orderRemark : value
                        },
                        type :'post',
                        async : false,
                        success : function(res){
                            if(res.success){
                                layer.msg(res.msg,{icon:1})
                                layer.closeAll()
                                table.reload('table')
                            }else{
                                layer.msg(res.msg,{icon:2})
                            }
                        }
                    });

                });
            }
        });
    }

    // 查看订单详情
    function detail(data){
        // 通过orderCode 查询子项的内容
        table.render({
            elem: '#detailTable'
            ,url: '/eng/waste/order/listPageItem?orderCode='+data.orderCode,
            page: true,
            height : '400px'
            ,cols: [[
                {field: 'id', title: 'ID', width:180, fixed: 'left',hide:true}
                ,{field: 'orderCode', title: '订单编号', width:240,align: 'center'}
                ,{field: 'wasteItemName', title: '类型名称', width:180,align: 'center'}
                ,{field: 'wasteTypeParamName', title: '型号', width:180,align: 'center'}
                ,{field: 'totalNum', title: '数量', width:80,align: 'center'}
                ,{field: 'singlePrice', title: '单价', width:80,align: 'center'}
                ,{field: 'totalPrice', title: '总价', width: 80,align: 'center'}
            ]]
        });
        layer.open({
            type :1,
            content :$("#detailLayer"),
            title : "订单详情",
            area :['1000px',"500px"]
        });
    }

    //toolbar
    table.on('toolbar(table)', function(obj){
        var event = obj.event;
        if(event == 'query'){

        }
    });

});